package com.xmcowell.sportpacemaster.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.xmcowell.sportpacemaster.R;
import com.xmcowell.sportpacemaster.utils.DensityUtil;

/**
 * Created by Administrator on 2017/3/16.
 */

public class UserFragmentItemView extends View {
    private int XPoint = 0;//原点X坐标
    private int YPoint = 0;//原点Y坐标
    private int XScale = 8;  //X轴每格刻度的长度
    private float YScale = 40;//Y轴每格刻度的长度
    private int max = 12;//Y轴的刻度个数
    private int width;//视图宽度
    private int height;//视图高度
    private float YHeight = 0;//进度条的高度
    private float progress = 0;

    private int TEXTSIZE = 10;//默认字体大小
    private int textSize;//字体大小
    private int top ;//顶部边距
    private int TOP = 0;

    private int imgHeight;

    private Context mContext;

    public UserFragmentItemView(Context context) {
        super(context);
//        this.mContext = context;
    }

    public UserFragmentItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.UserFragmentItem);
        textSize = ta.getInteger(R.styleable.UserFragmentItem_item_textSize, TEXTSIZE);
        top = ta.getInteger(R.styleable.UserFragmentItem_item_top,TOP);

        ta.recycle();
    }

    public UserFragmentItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        width = getWidth();
        height = getHeight();
        YScale = (height) / max;
        YHeight = YScale * (max-progress);



        //空心矩形
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(getResources().getColor(R.color.color_666));
        paint.setStrokeWidth(2);
        canvas.drawRect(XPoint, YPoint + top, width, height, paint);
        //实心矩形
        Paint paint1 = new Paint();
        paint1.setStyle(Paint.Style.FILL);
        paint1.setColor(getResources().getColor(R.color.color_666));
        canvas.drawRect(3, YHeight+8, width - 3, height, paint1);
//        DensityUtil.dip2px(mContext, 2)
        //文字
        Paint paint2 = new Paint();
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setAntiAlias(true);//去锯齿
        paint2.setColor(getResources().getColor(R.color.distance_out_color));
        paint2.setTextSize(textSize);
        canvas.drawText(progress + "", 6, YHeight + 6, paint2);

    }

    /**
     * 设置Y轴最大值
     *
     * @param max
     */
    public void setYLabel(int max) {
        this.max = max / 10;
        this.invalidate();
    }

    public void setProgress(int progress) {
        this.progress = (float) progress / 10;
        this.invalidate();
    }

}
